<?php
    /* Fields
    order_id,
	part_number,
	description	,
	model_id,
	equipment_id,
	company_id,
	last_ordered,
	date_expected,
	man_part_number,
	cost,
	agent,
	tracking_number,
	return_auth,
	credit_memo,
	date_returned
	*/
function reequipment_user_orders()
{
	// reminder, as you add phases, they need to be added to the allowed input.
    if (!xarVarFetch('phase', 'enum:form:create:delete:edit:update:create_invoice:ajax_update', $phase, 'form', XARVAR_NOT_REQUIRED, XARVAR_PREP_FOR_DISPLAY)) return;
    if (!xarVarFetch('order_id', 'int:1:', $order_id, '', XARVAR_NOT_REQUIRED)) return;
    if (!xarVarFetch('part_number', 'str:1:', $part_number, '', XARVAR_NOT_REQUIRED)) return;
    if (!xarVarFetch('po', 'str:1:', $po, '', XARVAR_NOT_REQUIRED)) return;
    if (!xarVarFetch('credit_memo', 'str:1:', $credit_memo, '', XARVAR_NOT_REQUIRED)) return;
    if (!xarVarFetch('description', 'str:1:', $description, '', XARVAR_NOT_REQUIRED)) return;
    if (!xarVarFetch('model_id', 'int:0:', $model_id, '', XARVAR_NOT_REQUIRED)) return;
    if (!xarVarFetch('equipment_id', 'int:0:', $equipment_id, '', XARVAR_NOT_REQUIRED)) return;
    if (!xarVarFetch('man_part_number', 'str:1:', $man_part_number, '', XARVAR_NOT_REQUIRED)) return;
    if (!xarVarFetch('last_ordered', 'int:1:', $last_ordered, '', XARVAR_NOT_REQUIRED)) return;
    if (!xarVarFetch('date_expected', 'str:1:', $date_expected, '', XARVAR_NOT_REQUIRED)) return;
    if (!xarVarFetch('agent', 'str:1:', $agent, '', XARVAR_NOT_REQUIRED)) return;
    if (!xarVarFetch('tracking_number', 'str:1:', $tracking_number, '', XARVAR_NOT_REQUIRED)) return;
    if (!xarVarFetch('return_auth', 'str:1:', $return_auth, '', XARVAR_NOT_REQUIRED)) return;
    if (!xarVarFetch('date_returned', 'str:1:', $date_returned, '', XARVAR_NOT_REQUIRED)) return;
    if (!xarVarFetch('cost', 'float', $cost, '', XARVAR_NOT_REQUIRED)) return;

    if (!xarSecurityCheck('EditEquipment', 1)) return;
    $data = array();
    xarTplSetPageTitle(xarML('Pending Orders'));

    // Get company_id
    if (!xarVarFetch('company_id', 'int:1:', $company_id, '', XARVAR_NOT_REQUIRED)) return;
	if (!xarModAPIFunc('reequipment', 'user', 'getcompanyid', array('company_id_from_url' => $company_id))) return;
	$company_id = xarSessionGetVar('company_id');

    switch(strtolower($phase)) {
        case 'form':
            // return var_export($data['schedules']);
            $data['form_label'] = xarML('Add Item');
            $data['form_phase'] = 'create';
            $data['form_order_id'] = '';
            $data['form_company_id'] = $company_id;
            $data['form_part_number'] = '';
            $data['form_description'] = '';
            $data['form_equipment_id'] = $equipment_id;
            $data['form_model_id'] = '';
            $data['form_man_part_number'] = '';
            $data['form_po'] = '';
            $data['form_cost'] = '';
            $data['form_credit_memo'] = '';
            $data['form_date_expected'] = '';
            $data['form_agent'] = '';
            $data['form_tracking_number'] = '';
            $data['form_return_auth'] = '';
            $data['form_date_returned'] = '';
        break;

        case 'create':
        		$postinfo = xarModAPIFunc('reequipment', 'admin', 'createorder', array('part_number' => $part_number,
        																											   'credit_memo' => $credit_memo,
        																											   'description' => $description,
        																											   'equipment_id' => $equipment_id,
        																											   'company_id' => $company_id,
        																											   'man_part_number' => $man_part_number,
        																											   'po' => $po,
        																											   'part_number' => $part_number,
        																											   'cost' => $cost,
        																											   'date_expected' => $date_expected,
        																											   'agent' => $agent,
        																											   'tracking_number' => $tracking_number,
        																											   'return_auth' => $return_auth,
        																											   'date_returned' => $date_returned));
        		return true;
        break;

        case 'delete':
        		// return $equipment_id;
                $postinfo = xarModAPIFunc('reequipment', 'admin', 'updateorder', array('order_id' => $order_id, 'company_id' => $company_id, 'closed' => true));
                xarResponseRedirect(xarModURL('reequipment', 'user', 'orders', array('company_id' => $company_id)));
        break;

        case 'create_invoice':
        		// return $equipment_id;
        		$invoice = xarModAPIFunc('reequipment', 'user', 'getorders', array('order_id' => $order_id, 'company_id' => $company_id));
                $postinfo = xarModAPIFunc('reequipment', 'admin', 'updateorder', array('order_id' => $order_id, 'company_id' => $company_id, 'closed' => true));
                $target = 'form';
                foreach ($invoice as $var){
                	xarResponseRedirect(xarModURL('reequipment', 'user', 'items', array('cost' => $var['cost'], 'part_number' => $var['part_number'], 'man_part_number' => $var['man_part_number'], 'equipment_id' => $var['equipment_id'], 'description' => $var['description']), NULL, $target));
                }

        break;

        case 'edit':
            $data['equipment'] = xarModAPIFunc('reequipment', 'user', 'getequipment', array('company_id' => $company_id));
            // Last 10 parts ordered
            $data['orders'] = xarModAPIFunc('reequipment', 'user', 'getorders', array('company_id' => $company_id, 'numitems' => '20', 'is_open' => true));
            //return var_export($data['equipment']);
            // Company Name
            $companies = xarModAPIFunc('reequipment', 'user', 'getrel', array('company_id' => $company_id));
            foreach($companies as $var){
            	$data['company_name'] = $var['company_name'];
            }
            // return var_export($data['schedules']);
            $data['form_label'] = xarML('Edit Order');
            $data['form_phase'] = 'update';

            $form_var = xarModAPIFunc('reequipment', 'user', 'getorders', array('order_id' => $order_id, 'company_id' => $company_id));
            // return var_export($form_var);
            foreach($form_var as $var){
				$data['form_order_id'] = $var['order_id'];
				$data['form_company_id'] = $var['company_id'];
				$data['form_part_number'] = $var['part_number'];
				$data['form_description'] = $var['description'];
				$data['form_equipment_id'] = $var['equipment_id'];
				$data['form_model_id'] = $var['model_id'];
				$data['form_po'] = $var['po'];
				$data['form_man_part_number'] = $var['man_part_number'];
				$data['form_cost'] = $var['cost'];
				$data['form_credit_memo'] = $var['credit_memo'];
				$data['form_date_expected'] = $var['date_expected'];
            	$data['form_agent'] = $var['agent'];
            	$data['form_tracking_number'] = $var['tracking_number'];
            	$data['form_return_auth'] = $var['return_auth'];
            	$data['form_date_returned'] = $var['date_returned'];
			}
        break;

		case 'update':
			if (empty($order_id)) {
				xarResponseRedirect(xarModURL('reequipment', 'user', 'orders', array('company_id' => $company_id)));
			}
			// Update the rest of the information.
			$postinfo = xarModAPIFunc('reequipment', 'admin', 'updateorder', array('order_id' => $order_id, 'part_number' => $part_number, 'description' => $description, 'equipment_id' => $equipment_id, 'company_id' => $company_id, 'po' => $po, 'man_part_number' => $man_part_number, 'cost' => $cost, 'date_expected' => $date_expected, 	'agent' => $agent, 'tracking_number' => $tracking_number, 'return_auth' => $return_auth, 'credit_memo' => $credit_memo,	 'date_returned' => $date_returned));
			// return $postinfo;
			xarResponseRedirect(xarModURL('reequipment', 'user', 'orders', array('company_id' => $company_id)));
        break;

		case 'ajax_update':
			$postinfo = xarModAPIFunc('reequipment', 'admin', 'updateorder', array('order_id' => $order_id, 'part_number' => $part_number, 'description' => $description, 'equipment_id' => $equipment_id, 'company_id' => $company_id, 'tracking_number' => $tracking_number, 'return_auth' => $return_auth, 'credit_memo' => $credit_memo));
			return;
        break;
    }
    // Return the output
    return $data;
}

?>